* clean encuesta condiciones de vida ECV
********************************************************************************
* prelims
********************************************************************************
{
clear all
cap log close
set more off

}
********************************************************************************
* clean survey data
********************************************************************************
{
* datos básicos del hogar
import delimited "$orig/ine/ecv/esudb18d.csv", encoding(UTF-8)clear

* rename
rename db010 Year
rename db020 Country
rename db030 HHID
rename db040 Region
rename db060 CSEC
rename db090 HHWeight
rename db100 Urban

* store
compress
save "$data/temp/esudb18d.dta", replace

* fichero de hogares
import delimited "$orig/ine/ecv/esudb18h.csv", encoding(UTF-8) clear

* rename
rename hb010 Year
la var Year "Year of the Survey"
rename hb020 Country
la var Country "Country"
rename hb030 HHID
la var HHID "Household ID"
rename hb050 Month
la var Month "Month of the Survey"
rename hb060 YearHHInt
la var YearHHInt "Year of the Interview"
rename hb070 PersID
la var PersID "Person ID"
rename hb080 HHHeadID
la var HHHeadID "HH Head ID"
rename hb090 HHHead2ID
la var HHHead2ID "HH Head 2 ID"

rename hb100 DurInt
la var DurInt "Duration of the Interview"
rename hy020 DispYIncHH
la var DispYIncHH "Disposable Year Income"
g IsNetDisYInc = (hy020_f < 20000)
g IsGrossDisYInc = (hy020_f >= 20000 & hy020_f < 30000)
rename hy023 YIncHHPreTrans
la var YIncHHPreTrans "Yearly Income in HH Pre-Transfers"
rename hy030n NetImpRent
rename hy040n NetRentalInc
rename hy010 GYIncHH
la var GYIncHH "Renta bruta total del hogar"
rename hy040g GRentalInc
la var GRentalInc "Renta bruta procedente del alquiler de una propiedad o terreno"
rename hy050g GSubsInc
la var GSubsInc "Ayuda por familia/hijos"
rename hy060g GSocAssInc
la var GSocAssInc "Ingresos por asistencia social"
rename hy070g GHousAInc
la var GHousAInc "Ayuda para vivienda"
rename hy080g GOtherTransf1
la var GOtherTransf1 "Transferencias periódicas monetarias percibidas de otros hogares"
rename hy081g GOtherTransf2
la var GOtherTransf2 "Transferencias periódicas monetarias percibidas de otros hogares (pensiones alimenticias a hijos o compensatorias a cónyuges)"
rename hy090g GInterestInc
la var GInterestInc "Intereses, dividendos y ganancias brutos de inversiones de capital en empresas no constituidas en sociedad"
rename hy100g GInterestPaid
la var GInterestPaid "Intereses pagados del préstamo para la compra de la vivienda principal"
rename hy110g GInc16yo
la var GInc16yo "Renta bruta percibida por los menores de 16 años"
rename hy120g PropTax
la var PropTax "Impuesto sobre el patrimonio"
rename hy130g TransOtherHH1
la var TransOtherHH1 "Transferencias periódicas monetarias abonadas a otros hogares"
rename hy131g TransOtherHH2
la var TransOtherHH2 "Transferencias periódicas monetarias abonadas a otros hogares (pensiones alimenticias a hijos o compensatorias a cónyuges)"
rename hy140g IncTax
la var IncTax "Impuesto sobre la renta y cotizaciones sociales"

rename hx040 HHMembers
la var HHMembers "Household members"
rename hx060 HHType
la var HHType "Household Type"
destring HHType, force replace
rename hx240 ConsUnits
la var ConsUnits "Consumption Units"

* HH Disposable income
rename vhrentaa NHHDispInc
la var NHHDispInc "Renta disponible total del hogar neta"
rename vhrentaaia NHHDispIncIR
la var NHHDispIncIR "Renta disponible total del hogar neta incluyendo el alquiler imputado"

* HH Pre-Tax Disposable Income
g GHHDispInc = NHHDispInc - GSubsInc - GSocAssInc + PropTax + IncTax
la var GHHDispInc "Renta disponible total del hogar bruta"

* number of adults
g HHAdults = .
replace HHAdults = 1 if HHType<=6|HHType==10
replace HHAdults = 2 if (HHType>6 & HHType<=13) & HHType != 9
replace HHAdults = HHMembers if HHType==9
replace HHAdults = HHMembers-1 if HHType==14 /*some error here*/
replace HHAdults = HHMembers if HHType==.
la var HHAdults "HH Adults"

* per adult and consumption unit and per month
foreach var in GHHDispInc NHHDispInc NHHDispIncIR {

	* per adult per month
	g `var'AdMo = `var' / (HHAdults * 12)
	la var `var'AdMo "`var' per Adult per Month"
	
	* per consumption unit per month
	g `var'CUMo = `var' / (ConsUnits * 12)
	la var `var'CUMo "`var' per CU per Month"

}

* add HH weight
merge 1:1 HHID using "$data/temp/esudb18d.dta"
assert _m==3
drop _m

* round weight
g WeightRound = round(HHWeight)

* store
compress
save "$data/temp/esudb18h.dta", replace

* calculate percentiles
foreach var in GHHDispInc GHHDispIncAdMo GHHDispIncCUMo NHHDispInc NHHDispIncAdMo NHHDispIncCUMo {

	* generate percentiles
	pctile `var'_tile = `var' [fw = WeightRound], nq(100)
	la var `var' "`var'"

}

g pctile = _n
la var pctile "Percentile"

* keep only variables of interest
keep *_tile pctile
keep if _n <= 99

* round
foreach var in GHHDispInc GHHDispIncAdMo GHHDispIncCUMo NHHDispInc NHHDispIncAdMo NHHDispIncCUMo {

	rename `var'_tile `var'
	replace `var' = round(`var', .01)

}

* lower and upper bounds
g IncTrhldMonth_l = GHHDispIncAdMo
g IncTrhldMonth_u = GHHDispIncAdMo[_n+1] - 0.01

* store
compress
save "$data/int/income_pctiles.dta", replace 

* erase temp files
cap erase "$data/temp/esudb18d.dta"
cap erase "$data/temp/esudb18h.dta"

}
********************************************************************************
* closing
********************************************************************************
{
cap log close
clear
}
